/*
Copyright (c) 2019 Alibaba Group Holding Limited

Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.

*/
//*******************************
//             MAILBOX
//*******************************
#include "mailbox.h"

/////////////////////////////////
//         t2r_data_box
/////////////////////////////////

/**
  * @brief  TEE向REE发送数据
  * @param  data: 写入的数组
  *         addr: 起始地址(offset)
  * @retval None
  */
void Tee_Send_Data2ree (u32* data,u32 addr)
{
  u32 len;
  u32 index;
  len = 20;
  for(index = 0;index < len;index++)
  {
    REG32(TBM_A_BASE + addr + index * 4) = *data;
    data++;
  }
}

/**
  * @brief  TEE读取REE发送给TEE的数据
  * @param  data: 需要发送的数据
  * @param  addr: 不加基址的偏移地址
  * @param  len:读取数据的数量,单位为字32bit
  * @retval data_p: 返回指向读取数据数组的指针
  */
void Tee_Read_Data_From_Ree (u32* data,u32 addr,u32 len)
{
  u32 index;
  for(index = 0;index < len;index++)
  {
    *data = REG32(RBM_B_BASE + index * 4);
    data++;
  }
}

/**
  * @brief  TEE读取TEE发送给REE的数据
  * @param  data: 需要发送的数据
  * @param  addr: 不加基址的偏移地址
  * @param  len:读取数据的数量,单位为字32bit
  * @retval data_p: 返回指向读取数据数组的指针
  */
void Tee_Read_Data_From_Tee (u32* data,u32 addr,u32 len)
{
  u32 index;
  for(index = 0;index < len;index++)
  {
    *data= REG32(TBM_A_BASE + index * 4);
    data++;
  }
}

/////////////////////////////////
//         r2t_data_box
/////////////////////////////////

/**
  * @brief  REE向TEE发送数据
  * @param  data: 写入的数组
  * @param  addr: 写入位置
  * @retval None
  */
void Ree_Send_Data2tee (u32* data,u32 addr)
{
  u32 len;
  u32 index;
  len = 20;
  for(index = 0;index < len;index++)
  {
    REG32(RBM_A_BASE + addr + index * 4) = *data;
    data++;
  }
}

/**
  * @brief  REE读取TEE发送给REE的数据
  * @param  data:数组指针
  * @param  addr: 不加基址的偏移地址
  * @param  len:读取数据的数量,单位为字32bit  
  * @retval data_p: 返回指向读取数据数组的指针
  */
void Ree_Read_Data_From_Tee (u32* data,u32 addr,u32 len)
{
  u32 index;
  for(index = 0;index < len;index++)
  {
    *data = REG32(TBM_B_BASE + addr + index * 4);
    data++;
  }
}

/**
  * @brief  TEE读取TEE发送给REE的数据
  * @param  data:数组指针
  * @param  addr: 不加基址的偏移地址
  * @param  len:读取数据的数量,单位为字32bit  
  * @retval data_p: 返回指向读取数据数组的指针
  */
void Ree_Read_Data_From_Ree (u32* data,u32 addr,u32 len)
{
  u32 index;
  for(index = 0;index < len;index++)
  {
    *data = REG32(RBM_A_BASE + index * 4);
    data++;
  }
}

/////////////////////////////////
//         t2r_cmd_box
/////////////////////////////////

/**
  * @brief  轮询REE进程是否完成
  * @param  none 
  * @retval none
  */

void Polling_Ree_Process_Done(void)
{
  while(REG32(REE2TEE_BASE+REE2TEE_DONE) != 0xaabbccdd)
  {};
}
/**
  * @brief  TEE发送事务完成标志
  * @param  none 
  * @retval none
  */

void Send_Tee_process_Done_Flag(void)
{
  Send_cmd_to_Tee_box(0xaabbccdd,TEE2REE_DONE);
}

/**
  * @brief  清除TEE发送事务完成标志
  * @param  none 
  * @retval none
  */

void Clean_Tee_process_Done_Flag(void)
{
  Send_cmd_to_Tee_box(0x00000000,TEE2REE_DONE);
}

/**
  * @brief  向信箱中写入命令
  * @param  cmd:命令
  * @param  offset_addr:地址
  * @retval none
  */

void Send_cmd_to_Tee_box(u32 cmd,u32 offset_addr)
{
  REG32(TEE2REE_BASE + offset_addr) &= !0xffffffff;
  REG32(TEE2REE_BASE + offset_addr) |= cmd;
}

/////////////////////////////////
//         r2t_cmd_box
/////////////////////////////////
/**
  * @brief  轮询TEE进程是否完成
  * @param  none 
  * @retval none
  */
void Polling_Tee_Process_Done(void)
{
  while(REG32(TEE2REE_BASE+TEE2REE_DONE) != 0xaabbccdd)
  {};
}

/**
  * @brief  REE发送事务完成标志
  * @param  none 
  * @retval none
  */

void Send_Ree_process_Done_Flag(void)
{
  Send_cmd_to_Ree_box(0xaabbccdd,REE2TEE_DONE);
}

/**
  * @brief  清除REE发送事务完成标志
  * @param  none 
  * @retval none
  */

void Clean_Ree_process_Done_Flag(void)
{
  Send_cmd_to_Tee_box(0x00000000,REE2TEE_DONE);
}

/**
  * @brief  向信箱中写入命令
  * @param  cmd:命令
  * @param  offset_addr:地址
  * @retval none
  */

void Send_cmd_to_Ree_box(u32 cmd,u32 offset_addr)
{
  REG32(REE2TEE_BASE + offset_addr) &= !0xffffffff;
  REG32(REE2TEE_BASE + offset_addr) |= cmd;
}